package com.league.zjxn.main.mine

import android.content.Context
import androidx.lifecycle.LifecycleOwner
import com.blankj.utilcode.util.ActivityUtils
import com.league.zjxn.R
import com.league.zjxn.jetpack.vmodel.UserViewModel
import com.league.zjxn.main.mine.adapter.ProjectApplyAdapter
import com.league.zjxn.main.popup.LoadingDialog
import com.module.lemlin.http.HttpResponseBody
import com.module.lemlin.http.Status
import com.module.lemlin.http.msg
import com.module.lemlin.owner.OwnerViewModeActivity
import kotlinx.android.synthetic.main.activity_project_apply.*

class ProjectApplyActivity : OwnerViewModeActivity<UserViewModel>() {

    companion object {
        fun open() {
            ActivityUtils.startActivity(ProjectApplyActivity::class.java)
        }
    }

    private val mProjectApplyAdapter = ProjectApplyAdapter()

    override fun viewModel(): UserViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.activity_project_apply

    override fun stateBar(): StatusBar {
        return StatusBar(view = flProjectApplyBar)
    }

    override fun initView() {
        rvProjectApplyBody.adapter = mProjectApplyAdapter

        rbProjectApplyCycle1.isChecked = true
        rbProjectApplyCycle1.setOnClickListener {
            viewModel.userProjectApply("1")
        }
        rbProjectApplyCycle2.setOnClickListener {
            viewModel.userProjectApply("2")
        }
        rbProjectApplyCycle3.setOnClickListener {
            viewModel.userProjectApply("3")
        }
        rbProjectApplyCycle4.setOnClickListener {
            viewModel.userProjectApply("4")
        }
    }

    override fun initData() {
        viewModel.userProjectApply("1")
    }

    override fun initViewMode() {
        viewModel.projectApplyLiveData.observe(this as LifecycleOwner, { response ->
            responseLoadingDialog(response)
            if (response.status != Status.SUCCESS) {
                return@observe
            }
            val data = response.data?.data?.data
                ?: return@observe
            mProjectApplyAdapter.setNewInstance(data)
        })
    }


    private fun responseLoadingDialog(response: HttpResponseBody<*>) {
        when (response.status) {
            Status.START -> LoadingDialog.show(
                this as Context, resId = R.layout.dialog_load_style_1
            )
            Status.FAILURE -> {
                LoadingDialog.dismiss()
                toast(response.error?.msg)
            }
            Status.SUCCESS -> {
                LoadingDialog.dismiss()
            }
        }
    }
}